package com.yjyw.tiexie.db;

import com.yjyw.tiexie.bean.ABean;
import com.yjyw.tiexie.bean.BBean;

import java.util.List;

public class DaoBUtils {
    private volatile static DaoBUtils instance;

    public CommonDaoUtils<BBean> userCommonDaoUtils;


    public static DaoBUtils getInstance() {
        synchronized (DaoBUtils.class) {
            if (instance == null) {
                instance = new DaoBUtils();
            }
        }

        return instance;
    }

    private DaoBUtils() {
        DaoManager mManager = DaoManager.getInstance();

        userCommonDaoUtils = new CommonDaoUtils(BBean.class, mManager.getDaoSession().getBBeanDao());

    }


    //新建表
    public void daoInsertDefaultUser() {
        if (daoQueryAllUser().size() == 0) {
            BBean user = new BBean();
            userCommonDaoUtils.insert(user);
        }
    }

    public void insertDefaultUser(BBean user) {
        userCommonDaoUtils.insert(user);
    }

    //插入记录数据存在则替换，数据不存在则插入
    public void insertOrReplace(BBean user) {
        userCommonDaoUtils.insertOrReplace(user);
    }


    //查询用户
    public List<BBean> daoQueryAllUser() {
        return userCommonDaoUtils.queryAll();
    }

    //查询用户
    public BBean daoQueryUser(long id) {
        return userCommonDaoUtils.queryById(id);
    }

    //查询用户
    public List<BBean> queryByNativeSql(String sql,String[] conditions) {
        return userCommonDaoUtils.queryByNativeSql(sql, conditions);
    }

    //删除用户
    public boolean deleteAllUser() {
        return userCommonDaoUtils.deleteAll();
    }

    //更新用户
    public boolean updateUser(BBean user) {
        return userCommonDaoUtils.update(user);
    }

}
